package com.leadnews.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leadnews.dtos.Filedto;
import com.leadnews.dtos.Publishdto;
import com.leadnews.mapper.LevelMapper;
import com.leadnews.mapper.MaterialFreeMapper;
import com.leadnews.mapper.WemediaMaterialMapper;
import com.leadnews.pojo.*;
import com.leadnews.service.MaterialFreeService;
import com.leadnews.service.UserService;
import com.leadnews.service.WemediaService;
import com.leadnews.mapper.WemediaMapper;
import com.leadnews.utils.TokenFilter;
import com.leadnews.vo.*;
import com.leadnews.vo.front.WemediaFront;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.leadnews.utils.AppJwtUtil.getClaimsBody;

/**
* @author 自己
* @description 针对表【wemedia】的数据库操作Service实现
* @createDate 2023-01-09 21:43:01
*/
@Service
@Slf4j
public class WemediaServiceImpl extends ServiceImpl<WemediaMapper, Wemedia>
    implements WemediaService{

@Autowired
MaterialFreeMapper materialFreeMapper;
@Autowired
WemediaMapper wemediaMapper;
@Autowired
    WemediaMaterialMapper wemediaMaterialMapper;
    @Override
    public ResponseResult publish(Publishdto publishdto,String id) {
        ResponseResult result = new ResponseResult();
        Wemedia wemedia = new Wemedia();
        wemedia.setStatus(0+"");
        wemedia.setUserId(id);
        wemedia.setIstop(0+"");
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        wemedia.setCreateTime(format);
        wemedia.setContent(publishdto.getContent());
        int insert = wemediaMapper.insert(wemedia);
        if (publishdto.getFiles().size()==0){
            result.setCode(200);
            result.setErrorMessage("发布成功，等待审核");
        }else {
            for (Filedto file : publishdto.getFiles()) {
                LambdaQueryWrapper<MaterialFree> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MaterialFree::getSizeimage, file.getSize())
                        .eq(MaterialFree::getName, file.getName())
                        .eq(MaterialFree::getUid, id);
                List<MaterialFree> materialFrees = materialFreeMapper.selectList(queryWrapper);
                MaterialFree materialFree = materialFrees.get(0);

                if (insert > 0) {
                    WemediaMaterial wemediaMaterial = new WemediaMaterial();
                    wemediaMaterial.setWemediaId(wemedia.getId());
                    wemediaMaterial.setUid(id);
                    wemediaMaterial.setUrl(materialFree.getUrl());
                    wemediaMaterial.setName(file.getName());
                    wemediaMaterial.setSizeimage(file.getSize());
                    int insert1 = wemediaMaterialMapper.insert(wemediaMaterial);
                    if (insert1 > 0) {
                        materialFreeMapper.deleteById(materialFree.getId());
                        result.setCode(200);
                        result.setErrorMessage("发布成功，等待审核");

                    } else {
                        result.setCode(0);
                        result.setErrorMessage("发布失败");

                    }
                }

            }
        }
        return result;
    }
@Autowired
LevelMapper userService;

    @Override
    public ResponseResult findAllWemedia(Map map) {
        ResponseResult result = new ResponseResult();
        Object id = null;
        try {
            Claims claimsBody = getClaimsBody((String) map.get("token"));
            id = claimsBody.get("id");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("token过期无效");
        }
        ArrayList<Object> arrayList = new ArrayList();
        System.out.println("解析token成功 ==> 用户的id值 == "+ id);
        Level user = userService.getById( id);
        if (user==null){
            result.setCode(300);
            result.setErrorMessage("no access ,please login");
            return result;
        }
        arrayList.add(user);
        List<Wemedia> wemediaList=wemediaMapper.findAllWemedia(id);
        for (Wemedia wemedia : wemediaList) {
            WemediaVo wemediaVo=new WemediaVo();
            List<String> pics=wemediaMaterialMapper.findPic(wemedia.getId());
            wemediaVo=userService.finduser(wemedia.getUserId());
            wemediaVo.setContent(wemedia.getContent());
            wemediaVo.setIstop(wemedia.getIstop());
            wemediaVo.setUrls(pics);
            wemediaVo.setStatus(wemedia.getStatus());
            wemediaVo.setCreateTime(wemedia.getCreateTime());
            wemediaVo.setWemediaId(wemedia.getId());
            arrayList.add(wemediaVo);
        }
        result.setData(arrayList);
        result.setCode(200);
        result.setErrorMessage("发布成功，请等待审核");
        System.out.println(result.getData());
        return result;
    }

    @Override
    public PageResultVo findWemediaFront(PageWemediaVo pageVo) {
        PageResultVo pageResultVo = new PageResultVo();
        String pageSize = pageVo.getPageSize();
        String pageNum = pageVo.getPageNum();
        String userId = pageVo.getUserId();
        String status = pageVo.getStatus();
        String istop = pageVo.getIstop();
        int i = Integer.parseInt(pageNum);
        int i1 = Integer.parseInt(pageSize);
        i=(i-1)*i1;
        System.out.println(pageVo);
        List<WemediaFront> wemedia;

        if (pageVo.getCreateTime()==null){
            wemedia=wemediaMapper.findWemediaFront(i,i1,userId,status,istop,"1111-01-10","9999-10-10");
        }else {
            wemedia = wemediaMapper.findWemediaFront(i, i1, userId, status, istop, pageVo.getCreateTime().get(0), pageVo.getCreateTime().get(1));
        }
        log.info("动态列表{}",wemedia);
        Integer integer = wemediaMapper.selectCount(new LambdaQueryWrapper<Wemedia>());
        pageResultVo.setTotal(integer);
        pageResultVo.setList(wemedia);
        return pageResultVo;
    }
}




